<!-- 域名加速 -->
<template>
  <div class="dashboard-editor-container">
    <el-row style="background: #fff; padding: 16px 16px 0">
      <div class="top topSubmitBtn">
        <el-button type="primary" @click="addomainName()">添加域名</el-button>
        <p>指定强制加速域名</p>
      </div>
      <div>
        <el-table stripe :data="tableData" style="width: 100%; margin: 15px 0">
          <el-table-column type="index" align="left" label="序号" width="80px"></el-table-column>
          <el-table-column prop="url" align="left" label="域名"></el-table-column>
          <el-table-column label="操作" align="left" width="100px">
            <template slot-scope="scope">
              <el-button size="mini" type="danger" @click="handleDelete(scope.row.url)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <el-dialog
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        title="添加域名"
        :visible.sync="dialogVisible"
        v-el-drag-dialog
        width="40%"
      >
        <div class="Dialog-title" style="margin-bottom: 10px">请输入域名:</div>
        <el-input v-model="omainName" :maxlength="500" oninput="value=value.replace(/[^a-zA-Z0-9-._~:/?#@!$&'()*+,;=%[\]]/g,'')"></el-input>
        <!--底部分割线 -->
        <el-divider class="lineToFooter"></el-divider>
        <span slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submit()">确 定</el-button>
          <el-button type="info" @click="dialogVisible = false">取 消</el-button>
        </span>
      </el-dialog>
    </el-row>
  </div>
</template>

<script>
import { connrequest } from "@/api/firstPage";
import elDragDialog from "@/directive/el-drag-dialog"; // base on element-ui
export default {
  directives: { elDragDialog },
  data() {
    return {
      dialogVisible: false,
      omainName: "",
      tableData: []
    };
  },
  //生命周期 - 创建完成（访问当前this实例）
  created() {
    this.getData();
  },
  //生命周期 - 挂载完成（访问DOM元素）
  mounted() {},
  methods: {
    async getData() {
      this.tableData = [];
      const res = await connrequest("GetV2rayURL");
      res.Data = res.Data || [];
      res.Data.map((item) => {
        this.tableData.push({ url: item });
      });
    },
    addomainName() {
      this.omainName = "";
      this.dialogVisible = true;
    },
    async submit() {
      // let isURL = /^(?=^.{3,255}$)(http(s)?:\/\/)?(www\.)?[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+(:\d+)*(\/\w+\.\w+)*([\?&]\w+=\w*)*$/
      // if( !isURL.test(this.omainName)){
      //   this.$message.error('域名格式错误')
      //   return false
      // }
      if (!this.omainName) {
        this.$message.error("域名不能为空");
        return false;
      }
      this.omainName = this.omainName.replace(/^https?\:\/\//i, "");
      if (this.tableData.find((item) => item.url === this.omainName) !== undefined) {
        this.$message.error("该域名已经在加速名单");
        return false;
      }
      let data = {
        Data: { URL: this.omainName }
      };
      const res = await connrequest("AddV2rayURL", data);
      if (res.Status.StatusCode === 0) {
        this.$message.success("域名添加成功");
        this.getData();
        this.dialogVisible = false;
      }
    },
    handleDelete(item) {
      this.$confirm("此操作将永久删除该域名, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async () => {
          let data = {
            Data: { URL: item }
          };
          const res = await connrequest("DelV2rayURL", data);
          if (res.Status.StatusCode === 0) {
            this.$message({
              type: "success",
              message: "删除成功!"
            });
            this.getData();
            this.dialogVisible = false;
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    }
  }
};
</script>
<style scoped lang="scss">
/* @import url(); 引入css类 */
.top {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  p {
    font-size: 15px;
    color: #409eff;
    padding-left: 20px;
  }
}
.night {
  //对话框Dialog的input旁边的字体颜色
  .Dialog-title {
    color: #fff !important;
  }
}
</style>
